package com.nlp.visualization.service.impl;

import com.nlp.visualization.service.IDataService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class DataServiceImpl implements IDataService {

    /**
     * 将长句切分为短句，按句为单位处理数据
     *
     * @param sentences
     * @return
     */
    public List<String> splitSentences(String sentences) {
        List<String> subSentences = new ArrayList<>();
        String regEx = "[。？！?.!;；]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(sentences);
        /*按照句子结束符分割句子*/
        String[] words = p.split(sentences);
        /*将句子结束符连接到相应的句子后*/
        if (words.length > 0) {
            int count = 0;
            while (count < words.length) {
                if (m.find()) {
                    words[count] += m.group();
                }
                count++;
            }
        }

        /*输出结果*/
        for (int index = 0; index < words.length; index++) {
            String word = words[index];
            subSentences.add(word);
        }
        return subSentences;

    }

//    public static void main(String[] args) {
//        DataServiceImpl d = new DataServiceImpl();
//        d.splitSentences("第一句。第二句！第三句：第四句；第五句。");
//    }

}
